package com.gph.saviorframework.mq.api;

import com.gph.saviorframework.mq.api.order.OrderConsumer;
import com.gph.saviorframework.mq.api.order.OrderProducer;
import com.gph.saviorframework.mq.api.transaction.LocalTransactionChecker;
import com.gph.saviorframework.mq.api.transaction.TransactionProducer;

import java.util.Properties;

/**
 * Created by root on 16-7-10.
 */
public class MQFactory {

    private static MQFactoryAPI mqFactory = null;

    static {
        try {
            Class<?> factoryClass =
                    MQFactory.class.getClassLoader().loadClass(
                            "com.gph.saviorframework.mq.api.impl.MQFactoryImpl");
            mqFactory = (MQFactoryAPI) factoryClass.newInstance();
        } catch (Throwable e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 创建Producer
     *
     * @param properties
     *         Producer的配置参数
     *
     * @return
     */
    public static Producer createProducer(final Properties properties) {
        return mqFactory.createProducer(properties);
    }

    /**
     * 创建Consumer
     *
     * @param properties
     *         Consumer的配置参数
     *
     * @return
     */
    public static Consumer createConsumer(final Properties properties) {
        return mqFactory.createConsumer(properties);
    }

    /**
     * 创建顺序消息生产者
     * @param properties
     * @return
     */
    public static OrderProducer createOrderProducer(final Properties properties){
        return mqFactory.createOrderProducer(properties);
    }

    /**
     * 创建顺序消息消费者
     * @param properties
     * @return
     */
    public static OrderConsumer createOrderConsumer(final Properties properties){
        return mqFactory.createOrderedConsumer(properties);
    }

    /**
     * 创建事务Producer
     *
     * @param properties
     *         Producer的配置参数
     *
     * @return
     */
    public static TransactionProducer createTransactionProducer(final Properties properties,
                                                                final LocalTransactionChecker checker) {
        return mqFactory.createTransactionProducer(properties, checker);
    }
}
